// pages/post-detail/post-detail.js

const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    post_detail:{},
    allPostData:[],
    allPostDatas:[],
    cart:[]
  },
  dianZan: function () {
    var thePostData1 = this.data.post_detail;
    if (thePostData1.upStatus == true) {
      thePostData1.upStatus = false;
      thePostData1.upNum--;
    } else {
      thePostData1.upStatus = true;
      thePostData1.upNum++;
    }
    this.setData({
      post_detail: thePostData1,
    });
    //更新缓存
    var allPostData1 = wx.getStorageSync('allPostData');
    var id = thePostData1.postId;
    for (var i = 0; i < allPostData1.length; i++) {
      if (id == allPostData1[i].postId) {
        allPostData1[i] = thePostData1;
        break;
      }
    }
    wx.setStorageSync('allPostData', allPostData1);
  },

  shoucang: function () {
    var thePostData2 = this.data.post_detail;
    if (thePostData2.upStatu == true) {
      thePostData2.upStatu = false;
      thePostData2.colletionNum--;
    } else {
      thePostData2.upStatu = true;
      thePostData2.colletionNum++;
    }
    this.setData({
      post_detail: thePostData2,
    });
    //更新缓存
    var allPostData2 = wx.getStorageSync('allPostDatas');
    var id = thePostData2.postId;
    for (var i = 0; i < allPostData2.length; i++) {
      if (id == allPostData2[i].postId) {
        allPostData2[i] = thePostData2;
        break;
      }
    }
    wx.setStorageSync('allPostDatas', allPostData2);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log("首页选择物体传递过来的id",options.id)
    var post_id = options.id - 1
    db.collection('allPostData')
    .get()
    .then(res=>{
      console.log(res.data[0].allPostData[post_id])
      that.setData({
        post_detail:res.data[0].allPostData[post_id]
      })
    })

  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})